package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TableRow;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.MySelfQABean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/16.
 */
public class MySelfQAAdapter extends BaseAdapter {

    private ArrayList<MySelfQABean> qAList;

    private Context context;

    public MySelfQAAdapter(Context context, ArrayList<MySelfQABean> qAList) {
        this.context = context;
        this.qAList = qAList;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return qAList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_of_q_a, null);
            holder = new ViewHolder();
            holder.contentText = (TextView) view.findViewById(R.id.item_of_q_a_content_text);

            holder.commentTableRow = (TableRow) view.findViewById(R.id.item_of_q_a_comment_row);
            holder.commentNumText = (TextView) view.findViewById(R.id.item_of_q_a_comment_text);

            holder.collectTableRow = (TableRow) view.findViewById(R.id.item_of_q_a_collect_row);
            holder.collectNumText = (TextView) view.findViewById(R.id.item_of_q_a_collect_text);

            holder.likeTableRow = (TableRow) view.findViewById(R.id.item_of_q_a_like_row);
            holder.likeNumText = (TextView) view.findViewById(R.id.item_of_q_a_like_text);

            holder.dateText = (TextView) view.findViewById(R.id.item_of_q_a_date_text);
            holder.timeText = (TextView) view.findViewById(R.id.item_of_q_a_time_text);

            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
            resetViewHolder(holder);
        }

        MySelfQABean bean = qAList.get(position);

        holder.contentText.setText(bean.getAnswerContent());

        //如果是草稿箱里的问答
        if (bean.getType().equals("draft_qa")) {
            holder.commentTableRow.setVisibility(View.GONE);
            holder.collectTableRow.setVisibility(View.GONE);
            holder.likeTableRow.setVisibility(View.GONE);
        }

        //如果是我的关注里的问答
        else {
            holder.commentTableRow.setVisibility(View.VISIBLE);
            holder.collectTableRow.setVisibility(View.VISIBLE);
            holder.likeTableRow.setVisibility(View.VISIBLE);

            holder.commentNumText.setText(bean.getCommentNumStr());

            holder.collectNumText.setText(bean.getCollectNumStr());

            holder.likeNumText.setText(bean.getLikeNumStr());
        }
        holder.dateText.setText(bean.getDateStr());

        holder.timeText.setText(bean.getTimeStr());

        return view;

    }

    private void resetViewHolder(ViewHolder holder) {
        holder.contentText.setText(null);
        holder.commentNumText.setText(null);
        holder.collectNumText.setText(null);
        holder.likeNumText.setText(null);
        holder.dateText.setText(null);
        holder.timeText.setText(null);
    }

    private class ViewHolder {
        private TextView contentText;

        private TableRow commentTableRow;

        private TextView commentNumText;

        private TableRow collectTableRow;

        private TextView collectNumText;

        private TableRow likeTableRow;

        private TextView likeNumText;

        private TextView dateText;

        private TextView timeText;
    }
}
